﻿using System;
using DataStore;
using DataStore.Domain;
using DataStore.Repositories;
using Infrastructure.CurrentData;
using Microsoft.Practices.Composite.Events;

namespace Infrastructure.ViewModels
{
    /// <summary>
    /// Publishing format viewmodel
    /// </summary>
    public class PublishingFormatViewModel: DataModelSingle<PublishingFormat>
    {
        /// <summary>
        /// Occurs when [updated].
        /// </summary>
        public event EventHandler<EventArgs> Updated;
        /// <summary>
        /// Called when [updated].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnUpdated(object sender, EventArgs e)
        {
            if (Updated != null)
            {
                Updated(sender, e);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PublishingFormatViewModel"/> class.
        /// </summary>
        /// <param name="item">The item.</param>
        public PublishingFormatViewModel(PublishingFormat item) : base(item)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PublishingFormatViewModel"/> class.
        /// </summary>
        /// <param name="eventAggregator">The event aggregator.</param>
        /// <param name="item">The item.</param>
        public PublishingFormatViewModel(IEventAggregator eventAggregator, PublishingFormat item) : base(eventAggregator, item)
        {
        }


        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public Guid Id
        {
            get
            {
                return Item.Id;
            }
            set
            {
                Item.Id = value;
                OnPropertyChanged("Id");
            }
        }
        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get
            {
                return Item.Text;
            }
            set
            {
                Item.Text = value;
                OnPropertyChanged("Text");
            }
        }
        /// <summary>
        /// Gets or sets the icon.
        /// </summary>
        /// <value>The icon.</value>
        public virtual ImageLink Icon
        {
            get
            {
                return Item.Icon;
            }
            set
            {
                Item.Icon = value;
                OnPropertyChanged("Icon");
            }
        }

        /// <summary>
        /// Deletes this instance.
        /// </summary>
        protected override void Delete()
        {
            IDatabase database = Current.Container.Resolve<IDatabase>();
            using (var unitOfWork = database.CreateUnitOfWork())
            {

                try
                {
                    IRepository<PublishingFormat> repository = new PublishingFormatRepository(unitOfWork);
                    unitOfWork.BeginTransaction();

                    repository.Delete(Item);

                    unitOfWork.CommitTransaction();
                }
                catch
                {
                    unitOfWork.RollbackTransaction();
                    throw;
                }
            }

            OnUpdated(this, EventArgs.Empty);
            base.Delete();
        }

        /// <summary>
        /// Gets a value indicating whether thye entity is a new item.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the entity is a new item; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsNewItem
        {
            get
            {
                return Item.Id == Guid.Empty;
            }
        }

        /// <summary>
        /// Saves the entity.
        /// </summary>
        public override void Save()
        {
            IDatabase database = Current.Container.Resolve<IDatabase>();
            using (var unitOfWork = database.CreateUnitOfWork())
            {

                try
                {
                    IRepository<PublishingFormat> repository = new PublishingFormatRepository(unitOfWork);
                    unitOfWork.BeginTransaction();

                    repository.SaveOrUpdate(Item);

                    unitOfWork.CommitTransaction();
                }
                catch
                {
                    unitOfWork.RollbackTransaction();
                    throw;
                }

            }


            OnUpdated(this, EventArgs.Empty);
            base.Save();
        }
    }
}